﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CDefineGlobal;
using System.Drawing;
using CObject;

namespace BUS_LAYER
{
    public class BUS_Map
    {
        static List<int> iRow = new List<int>();
        static List<int> iCol = new List<int>();
        static CChiHuy chiHuy = new CChiHuy();

        public static CChiHuy ChiHuy
        {
            get { return chiHuy; }
            set { chiHuy = value; }
        }

        public static List<int> IRow
        {
            get { return iRow; }
            set { iRow = value; }
        }

        public static List<int> ICol
        {
            get { return iCol; }
            set { iCol = value; }
        }

        public static void CheckExplosionBrick(int x, int y, CMap map)
        {
            int c1, r1, c2, r2, c3, r3, c4, r4;  // maze object row and column corners

            c1 = BUS_Utility.XScreenToXMaze(x); r1 = BUS_Utility.YScreenToYMaze(y);
            c2 = BUS_Utility.XScreenToXMaze(x + CDefine.TILE_SIZE - 1); r2 = BUS_Utility.YScreenToYMaze(y);
            c3 = BUS_Utility.XScreenToXMaze(x); r3 = BUS_Utility.YScreenToYMaze(y + CDefine.TILE_SIZE - 1);
            c4 = BUS_Utility.XScreenToXMaze(x + CDefine.TILE_SIZE - 1); r4 = BUS_Utility.YScreenToYMaze(y + CDefine.TILE_SIZE - 1);

            try
            {
                if (map.IMap.ArrTile[r1, c1] == CDefine.TILE_BRICK)
                {
                    map.IMap.ArrTile[r1, c1] = 0;
                    IRow.Add(r1);
                    ICol.Add(c1);
                }
                if (map.IMap.ArrTile[r2, c2] == CDefine.TILE_BRICK)
                {
                    map.IMap.ArrTile[r2, c2] = 0;
                    IRow.Add(r2);
                    ICol.Add(c2);
                }
                if (map.IMap.ArrTile[r3, c3] == CDefine.TILE_BRICK)
                {
                    map.IMap.ArrTile[r3, c3] = 0;
                    IRow.Add(r3);
                    ICol.Add(c3);
                }
                if (map.IMap.ArrTile[r4, c4] == CDefine.TILE_BRICK)
                {
                    map.IMap.ArrTile[r4, c4] = 0;
                    IRow.Add(r4);
                    ICol.Add(c4);
                }
            }
            catch (Exception)
            {
            }
        }

        public static void LoadMap(CMap map, List<AbstractTile> arrTile)
        {
            //khởi động lại arrTile
            arrTile.Clear();
            CBrick brick = new CBrick();
            CBush bush = new CBush();
            CStone stone = new CStone();

            for (int i = 0; i < map.IMap.Row; i++)
            {
                for (int j = 0; j < map.IMap.Col; j++)
                {
                    int a = map.IMap.ArrTile[i, j];

                    switch (a)
                    {
                        case 0:
                            continue;

                        case CDefine.TILE_BRICK:
                            CBrick br = (CBrick)brick.Clone();
                            br.ChangeLocation(j * br.GetWidth(), i * br.GetHeight());
                            arrTile.Add(br);
                            break;

                        case CDefine.TILE_BUSH:
                            CBush bu = (CBush)bush.Clone();
                            bu.ChangeLocation(j * bu.GetWidth(), i * bu.GetHeight());
                            arrTile.Add(bu);
                            break;

                        case CDefine.TILE_STONE:
                            CStone st = (CStone)stone.Clone();
                            st.ChangeLocation(j * st.GetWidth(), i * st.GetHeight());
                            arrTile.Add(st);
                            break;

                        case CDefine.TILE_CHI_HUY:
                            //kích thước chia cho 2 vì bitmap chỉ huy có kích thước gấp đôi các tile khác
                            ChiHuy.ChangeLocation(j * (ChiHuy.GetWidth() / 2), i * (ChiHuy.GetHeight() / 2));
                            arrTile.Add(ChiHuy);
                            break;
                    }//end switch
                }//end for
            }//end for
        }//end func

        public static void DrawMap(CMap map, List<AbstractTile> arrTile, Graphics g, Bitmap bitmapTemp)
        {
            LoadMap(map, arrTile);

            //cập nhật lại iRow và iCol
            IRow.Clear();
            ICol.Clear();

            //Vẽ arrTile lên graphics g
            for (int p = 0; p < arrTile.Count; p++)
                g.DrawImage(arrTile[p].Picture, arrTile[p].Location);
        }//end func

        public static void UpdateMap(Graphics g)
        {
            //vẽ lại vị trí gạch bị bắn (tô đen chỗ đó)
            for (int i = 0; i < ICol.Count; i++)
                g.FillRectangle(Brushes.Black, ICol[i] * CDefine.TILE_SIZE, IRow[i] * CDefine.TILE_SIZE, CDefine.TILE_SIZE, CDefine.TILE_SIZE);

            //vẽ lại chỉ huy nếu chỉ huy die
            g.DrawImage(ChiHuy.Picture, ChiHuy.Location);
        }
    }
}
